package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.Gwcq002Export;
import cn.e4j.modular.erp.service.impl.T100ServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 车辆发车明细查询
 *
 * @author miaoyl
 * @Date 2019/12/10
 */
@Controller
@RequestMapping("/crm/gwcq002")
public class Gwcq002Controller extends BaseController {

    @Autowired
    private T100ServiceImpl t100Service;

    @Autowired
    private SysAuthorityService sysAuthorityService;

    /**
     * 跳转页面
     */
    @RequiresPermissions("crm:gwcq002:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwcq002.html";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping("/getGwcq002List")
    @RequiresPermissions("crm:gwcq002:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        String ORG_ID = shiroUser.getORG_ID();
//        if(sysAuthorityService.getNolimitOrg(ORG_ID,null) == null ){
//            //客户权限
//            inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
//            //部门权限
//            List<Dto> orgs = sysAuthorityService.getOrgsAuthority(inDto);
//            if(orgs.size() > 0) {
//                orgs.removeIf(orgDto -> orgDto.getString("ID").equals(ORG_ID));
//            }
//            inDto.put("CURR_USER",shiroUser.getID());
//            inDto.put("ORGS", orgs);
//        }
        List<Dto> result = InvokeUtil.invokeBeanMethod("gwcq002Mapper,getGwcq002List", page, inDto);
        return new PageReData(page, result);
    }

    @RequestMapping(value = "/exportsData", method = RequestMethod.GET)
    @RequiresPermissions("crm:gwcq002:init")
    public void exportsData(ModelMap map, HttpServletRequest request, HttpServletResponse response, @DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        String ORG_ID = shiroUser.getORG_ID();
//        if(sysAuthorityService.getNolimitOrg(ORG_ID,null) == null ){
//            //客户权限
//            inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
//            //部门权限
//            List<Dto> orgs = sysAuthorityService.getOrgsAuthority(inDto);
//            if(orgs.size() > 0) {
//                orgs.removeIf(orgDto -> orgDto.getString("ID").equals(ORG_ID));
//            }
//            inDto.put("CURR_USER",shiroUser.getID());
//            inDto.put("ORGS", orgs);
//        }
        List<Map<String, Object>> list = InvokeUtil.invokeBeanMethod("gwcq002Mapper,getGwcq002List", inDto);
        JSONArray json_arr = new JSONArray();
        for (Map<String, Object> map1 : list) {
            JSONObject json_obj = new JSONObject();
            for (Map.Entry<String, Object> entry : map1.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                json_obj.put(key, value);
            }
            json_arr.add(json_obj);
        }
        List<Gwcq002Export> gwcq002Exports = new ArrayList<>();
        gwcq002Exports = JSONObject.parseArray(json_arr.toString(), Gwcq002Export.class);
        ExportParams params = new ExportParams(null, "车辆发车相关明细", ExcelType.XSSF);
        //params.setFreezeCol(2);
        map.put(NormalExcelConstants.FILE_NAME, "车辆发车相关明细");
        map.put(NormalExcelConstants.DATA_LIST, gwcq002Exports);
        map.put(NormalExcelConstants.CLASS, Gwcq002Export.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
}
